local
Type
command
Summary
Declares one or more local variables and assigns initial values to them.
Syntax
local <variableName> [= <value>] [, <variableName> [= <value>] ...]
Description
Use the local command to define a local variable for a handler, or to define a script local variable that is shared between all the handlers in a script.
A local's, optional, initial value can be anything, including arrays, as long as it can be defined by an expression when the script is compiled. The majority of operators and functions can be used, as long as their operands and arguments are literals or other (previously defined) constants.
If the expression uses functions or operators which use local properties, then the properties will be taken to be the default values, regardless of where the local is defined.
The local declaration is optional. You can use a local variable without declaring it first in the handler. In this case, the local variable can be used only within that handler.
The local command can appear anywhere in a handler. However, variable declarations are usually placed at the beginning of a handler. This makes them easier to find and as local variables are created at the point in the script where they are declared it will ensure the variable isn't used before the declaration.
You can also use the local command to create a script local variable. Script local variables can be used by any handler in that script, without needing to be declared in the handler itself, and their values are maintained from handler to handler. You create a script local variable by using the local command in a script, outside any handlers in the script. (The difference between a script local variable and a global variable is that a script local variable can only be used in the handlers of one script, while a global|global variable can be used in any handler or script with a global declaration for that variable.)
The value of a local variable is retained only while the handler is running. When the handler exits, the value of the local variable is lost.
The value of a script local variable is retained between handlers, but is lost when you quit the application, when you close the stack (unless its destroyStack property is false). If variable preservation is turned on, script locals retain their values when the script is re-compiled, unless there is an error in the script. With variable preservation turned off, script locals lose their values when the script is recompiled.
It is recommended that naming conventions are followed for variables in Livecode scripts. For handler local variables prefix with a lowercase 't' and for script local variables prefix with a lowercase 's' (as in tMyVariable and sMyVariable). This enables anyone reading the code to identify the scope of a variable when it is used, without having to find where it is declared
Parameters
Name | Type | Description |
---|---|---|
variableName | The variableName is any identifier. | |
value | Optional; The value is any constant expression. You can specify just the variable name. In this case, the specified local variables contain empty when created. |
Examples
local currentStatus -- creates one local variable
local thisThing, thatThing, theOtherThing -- creates 3 variables
local A=1,B=2,C=3 -- creates variables with initial values
-- To make a numbered list of variables:
repeat with tNum = 1 to 20
do "local tVar_" & tNum & "; put empty into tVar_" & tNum
end repeat
local piBy2 = pi / 2
local mapVar = { "a": 1, "b": 1 + 2 }
Related
command: constant, do, delete variable, global
function: variableNames, localNames
glossary: property, variable, handler, value, global, local variable, declare, script local variable, command, scope, local property
property: preserveVariables, script, explicitVariables
Compatibility and Support
Introduced
LiveCode 1.0
OS
mac
windows
linux
ios
android
Platforms
desktop
server
mobile